package 代码随想录.双指针法;

public class _7链表相交 {
    class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }
    /**
     * 面试题 02.07. 链表相交
     * https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/
     * 给你两个单链表的头节点 headA 和 headB ，请你找出并返回两个单链表相交的起始节点。如果两个链表没有交点，返回 null 。
     */
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode p1 = headA;
        ListNode p2 = headB;
        while(p1!=p2){
            p1 = p1!=null?p1.next:headB;
            p2 = p2!=null?p2.next:headA;
        }
        return p1;
    }
}
